package history.study.list;

import history.hot100.ListNode;

// 203. 移除链表元素
public class LeetCode_203 {
    public ListNode removeElements(ListNode head, int val) {
        if (head == null) return head;
        while (head !=null && head.val == val) head = head.next;
        if (head == null || head.next ==null) return head;
        ListNode slow =head, fast = head.next;
        while (fast!=null) {
            if (fast.val == val) {
                if (fast.next != null) {
                    slow.next = fast.next;
                    fast = fast.next;
                } else {
                    slow.next = null;
                    break;
                }
            } else {
                slow = slow.next;
                fast = fast.next;
            }
        }
        return head;
    }
}
